import 'package:shared_preferences/shared_preferences.dart';

enum Type { int, string, double, list, map }

class LocalStorage {
  static set(String key, value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if (value is String) {
      prefs.setString(key, value);
    } else if (value is num) {
      prefs.setInt(key, value);
    } else if (value is double) {
      prefs.setDouble(key, value);
    } else if (value is bool) {
      prefs.setBool(key, value);
    } else if (value is List) {
      // value = jsonEncode(value);
      prefs.setStringList(key, value.cast<String>());
    }
  }

  /// 返回数据data -> replace -> null
  static getStorage(String key, [dynamic replace]) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var data = prefs.get(key);
    return data ?? replace ?? null;
  }

  static removeKey(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.remove(key);
  }

  static removeAll() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.clear();
  }

  static containsKey(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool flag = prefs.containsKey(key);
    return flag;
  }
}
